﻿/// 859.亲密字符串 _859_buddy_strings
/// 给定两个由小写字母构成的字符串 A 和 B ，
/// 只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果，
/// 就返回 true ；否则返回 false 。

using System;
using System.Collections.Generic;

namespace _859_buddy_strings
{
    class Program
    {
        static void Main (string[] args)
        {
            Console.WriteLine (new Solution ().BuddyStrings ("aab", "aab"));
        }
    }

    public class Solution
    {
        public bool BuddyStrings (string A, string B)
        {
            if (A.Length != B.Length) return false;

            if (A.Length < 2 || B.Length < 2) return false;

            Dictionary<char, int> dicA = new Dictionary<char, int> ();

            List<int> num = new List<int> ();
            for (int i = 0;i < A.Length;i++)
            {
                if (A[i] != B[i]) num.Add (i);
                if (!dicA.ContainsKey (A[i])) dicA.Add (A[i], 1);
                else dicA[A[i]]++;
                if (!dicA.ContainsKey (B[i])) dicA.Add (B[i], 1);
                else dicA[B[i]]++;
            }

            if (num.Count != 2 && num.Count != 0) return false;

            if (num.Count == 2)
            {
                if (A[num[0]] != B[num[1]]) return false;
                if (A[num[1]] != B[num[0]]) return false;
                return true;
            }

            if (num.Count == 0)
            {
                foreach (var item in dicA)
                {
                    if (item.Value >= 4) return true;
                }
            }

            return false;
        }
    }
}
